/**
 *
 * @author Doan Tuan Anh
 */
import java.util.Timer;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import java.io.IOException;

public class StartCanvas extends GameCanvas {
	        
      static final int CORNER_X = 0;
      static final int CORNER_Y = 0;

      static int DISP_WIDTH;
      static int DISP_HEIGHT;

      public static final int BLACK = 0;
      public static final int WHITE = 0xffffff;

      private Display myDisplay;
      private GameMidlet gameMidlet;
      private Image welcomeImage, welcomeBackground;
      private Image startButtonImage, startButton;
      private final int BUTTON_WIDTH = 100;
      private final int BUTTON_HEIGHT = 30;
      private GameMidlet midlet;
      private MyGameCanvas gameCanvas;
      
      public StartCanvas(GameMidlet midlet, MyGameCanvas gameCanvas) throws Exception {
        super(false);
        
        this.midlet = midlet;
        this.gameCanvas = gameCanvas;
        
        myDisplay = Display.getDisplay(midlet);
        gameMidlet = midlet;

        DISP_WIDTH = getWidth();
        DISP_HEIGHT = getHeight();
            
        Display disp = Display.getDisplay(gameMidlet);
        if(disp.numColors() < 256) {
          throw(new Exception("game requires 256 shades"));
        }
        if((DISP_WIDTH < 150) || (DISP_HEIGHT < 170)) {
          throw(new Exception("Screen too small"));
        }

        welcomeImage = Image.createImage("/playscreen.png");
        welcomeBackground = resizeImage(welcomeImage, DISP_WIDTH, DISP_HEIGHT);
        
        startButtonImage = Image.createImage("/playbtn.png");
        startButton = resizeImage(startButtonImage, BUTTON_WIDTH, BUTTON_HEIGHT);
      }
      
      Image resizeImage(Image image, int resizedWidth, int resizedHeight) {  
          int[] in = null, out = null;  

          int width = image.getWidth(), height = image.getHeight();  
          in = new int[width];   

          int i  =0;  
          int dy, dx;   
          out = new int[resizedWidth * resizedHeight];   

          for (int y = 0; y < resizedHeight; y++)   
          {   
            dy = y * height / resizedHeight;  
            image.getRGB(in,0,width,0,dy,width,1);   
            for (int x = 0; x < resizedWidth; x++)   
            {   
              dx = x * width / resizedWidth;   

              out[(resizedWidth * y) + x] = in[dx];  
            }   
          }   
          Image resized = Image.createRGBImage(out,resizedWidth,resizedHeight,true);  
          return resized;   
    }  

        protected void pointerPressed(int x, int y)
        {
            int left = DISP_WIDTH / 2 - BUTTON_WIDTH / 2;
            int right = DISP_WIDTH / 2 + BUTTON_WIDTH / 2;
            int up = DISP_HEIGHT / 2;
            int down = DISP_HEIGHT / 2 + BUTTON_HEIGHT;
            
            if(left <= x && x <= right && up <= y && y <= down)
            {
                try
                {
                    if(gameCanvas != null) {
                            gameCanvas.start();
                            midlet.removeGoCommand();
                            midlet.addPauseCommand();
                            midlet.addNewCommand();
                            gameCanvas.flushKeys();
                            midlet.systemStartThreads();
                        }
                }
                catch(Exception e) {}
            }
            
        }
        
      void setSize()
      {
          DISP_WIDTH = getWidth();
          DISP_HEIGHT = getHeight();
      }
      
      public void paint(Graphics g) {
        boolean changedOrientation = false;
        
        if(DISP_WIDTH != getWidth()) 
        {
            changedOrientation = true;
            setSize();
        }
        
        g.setColor(WHITE);
        g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
        
        if(changedOrientation)
        {
            welcomeBackground = resizeImage(welcomeImage, DISP_WIDTH, DISP_HEIGHT);
            startButton = resizeImage(startButtonImage, BUTTON_WIDTH, BUTTON_HEIGHT);
        }
        
        g.drawImage(welcomeBackground, getWidth(), getHeight(), g.BOTTOM | g.RIGHT);
        g.drawImage(startButton, getWidth() / 2 + BUTTON_WIDTH / 2, getHeight() / 2 + BUTTON_HEIGHT, g.BOTTOM | g.RIGHT);
      }
}